/*
 * Copyright (C) 2008
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.googlecode.sca4idea.facet;

import com.intellij.facet.Facet;
import com.intellij.facet.FacetModel;
import com.intellij.facet.FacetType;
import com.intellij.facet.FacetTypeId;
import com.intellij.facet.ui.DefaultFacetSettingsEditor;
import com.intellij.facet.autodetecting.FacetDetector;
import com.intellij.facet.autodetecting.FacetDetectorRegistry;
import com.intellij.openapi.fileTypes.StdFileTypes;
import com.intellij.openapi.module.JavaModuleType;
import com.intellij.openapi.module.Module;
import com.intellij.openapi.module.ModuleType;
import com.intellij.openapi.roots.ModifiableRootModel;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.openapi.project.Project;
import static com.intellij.patterns.PlatformPatterns.virtualFile;
import static com.intellij.patterns.StandardPatterns.string;
import com.googlecode.sca4idea.SCABundle;
import com.googlecode.sca4idea.util.SCAIcons;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;
import java.util.Collection;

/**
 * Type used for the SCAFacet.
 */
public class SCAFacetType extends FacetType<SCAFacet, SCAFacetConfiguration> {
    public static final FacetTypeId<SCAFacet> ID = new FacetTypeId<SCAFacet>("sca");
    public static final SCAFacetType INSTANCE = new SCAFacetType();
    
    private SCAFacetType() {
        super(ID, "sca", SCABundle.message("facet.type.name.sca"));
    }

    public SCAFacetConfiguration createDefaultConfiguration() {
        return new SCAFacetConfiguration();
    }

    @Override
    public DefaultFacetSettingsEditor createDefaultConfigurationEditor(@NotNull Project project, @NotNull SCAFacetConfiguration config) {
        return new DefaultFacetConfigurationEditor(config);
    }

    public SCAFacet createFacet(@NotNull Module module, String name, @NotNull SCAFacetConfiguration configuration,
                                @Nullable Facet underlyingFacet) {
        return new SCAFacet(this, module, name, configuration, underlyingFacet);
    }

    /**
     * Determines if the given module is relevant for the SCA facet by detecting whether it is a Java module.
     *
     * @param moduleType the type of the module.
     * @return true if the module is a Java module.
     */
    public boolean isSuitableModuleType(ModuleType moduleType) {
        return moduleType instanceof JavaModuleType;
    }

    /**
     * Retrieves the icon to use for the facet.
     * @return the icon.
     */
    @Override
    public Icon getIcon() {
        return SCAIcons.SERVICE_ICON;
    }

    /**
     * Indicates that only one SCA Facet is allowed per module.
     * @return true to indicate that only one SCA Facet is allowed per module
     */
    @Override
    public boolean isOnlyOneFacetAllowed() {
        return true;
    }

    @Override
    public void registerDetectors(FacetDetectorRegistry<SCAFacetConfiguration> registry) {
        registry.registerUniversalDetector(
                StdFileTypes.XML, virtualFile().withName(string().endsWith(".composite")),
                new SCAFacetDetector());
    }

    private static class SCAFacetDetector extends FacetDetector<VirtualFile, SCAFacetConfiguration> {
        public SCAFacetDetector() {
            super("sca-detector");
        }

        public SCAFacetConfiguration detectFacet(final VirtualFile source,
                                                 final Collection<SCAFacetConfiguration> existentFacetConfigurations) {
            if (!existentFacetConfigurations.isEmpty()) {
                return existentFacetConfigurations.iterator().next();
            }
            return new SCAFacetConfiguration();
        }

        @Override
        public void beforeFacetAdded(@NotNull final Facet facet,
                                     final FacetModel facetModel, @NotNull final ModifiableRootModel modifiableRootModel) {
        }

    }
}
